package cn.iocoder.an.module.business.controller.admin.audittype;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.audittype.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.audittype.AuditTypeDO;
import cn.iocoder.an.module.business.dal.dataobject.audittype.AuditConfigDO;
import cn.iocoder.an.module.business.service.audittype.AuditTypeService;

@Tag(name = "管理后台 - 审核类型配置")
@RestController
@RequestMapping("/business/audit-type")
@Validated
public class AuditTypeController {

    @Resource
    private AuditTypeService auditTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建审核类型配置")
    @PreAuthorize("@ss.hasPermission('business:audit-type:create')")
    public CommonResult<Long> createAuditType(@Valid @RequestBody AuditTypeSaveReqVO createReqVO) {
        return success(auditTypeService.createAuditType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核类型配置")
    @PreAuthorize("@ss.hasPermission('business:audit-type:update')")
    public CommonResult<Boolean> updateAuditType(@Valid @RequestBody AuditTypeSaveReqVO updateReqVO) {
        auditTypeService.updateAuditType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核类型配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:audit-type:delete')")
    public CommonResult<Boolean> deleteAuditType(@RequestParam("id") Long id) {
        auditTypeService.deleteAuditType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核类型配置")
                @PreAuthorize("@ss.hasPermission('business:audit-type:delete')")
    public CommonResult<Boolean> deleteAuditTypeList(@RequestParam("ids") List<Long> ids) {
        auditTypeService.deleteAuditTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审核类型配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:audit-type:query')")
    public CommonResult<AuditTypeRespVO> getAuditType(@RequestParam("id") Long id) {
        AuditTypeDO auditType = auditTypeService.getAuditType(id);
        return success(BeanUtils.toBean(auditType, AuditTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审核类型配置分页")
    @PreAuthorize("@ss.hasPermission('business:audit-type:query')")
    public CommonResult<PageResult<AuditTypeRespVO>> getAuditTypePage(@Valid AuditTypePageReqVO pageReqVO) {
        PageResult<AuditTypeDO> pageResult = auditTypeService.getAuditTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuditTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得审核类型配置列表")
    public CommonResult<List<AuditTypeRespVO>> getAuditTypePage() {
        List<AuditTypeDO> list = auditTypeService.getAuditTypeList();
        return success(BeanUtils.toBean(list, AuditTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核类型配置 Excel")
    @PreAuthorize("@ss.hasPermission('business:audit-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditTypeExcel(@Valid AuditTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditTypeDO> list = auditTypeService.getAuditTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核类型配置.xls", "数据", AuditTypeRespVO.class,
                        BeanUtils.toBean(list, AuditTypeRespVO.class));
    }

    // ==================== 子表（审核项） ====================

    @TransMethodResult
    @GetMapping("/audit-config/list-by-audit-type-id")
    @Operation(summary = "获得审核项列表")
    @Parameter(name = "auditTypeId", description = "分类")
    @PreAuthorize("@ss.hasPermission('business:audit-type:query')")
    public CommonResult<List<AuditConfigDO>> getAuditConfigListByAuditTypeId(@RequestParam("auditTypeId") Long auditTypeId) {
        return success(auditTypeService.getAuditConfigListByAuditTypeId(auditTypeId));
    }


    @GetMapping("/audit-config/list-by-audit-type-id-wit")
    @Operation(summary = "获得审核项列表")
    @Parameter(name = "auditTypeId", description = "分类")
    @Parameter(name = "levelId", description = "级别ID", required = false) // 可选
    public CommonResult<List<AuditConfigDO>> getAuditConfigListByAuditTypeIdWait(
            @RequestParam("auditTypeId") Long auditTypeId,
            @RequestParam(value = "levelId") Long levelId) {
        return success(auditTypeService.getAuditConfigListByAuditTypeIdWait(auditTypeId, levelId));
    }

}